<script setup>
import { computed, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from './stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const showAdminMenu = ref(false)
const showOrderMenu = ref(false)

// 检查用户是否为管理员
const isAdmin = computed(() => {
  return userStore.user && userStore.hasRole('ADMIN')
})

// 判断当前是否为登录或注册页面
const isAuthPage = computed(() => {
  return ['/login', '/register'].includes(route.path)
})

// 切换管理员菜单显示状态
const toggleAdminMenu = () => {
  showAdminMenu.value = !showAdminMenu.value
  showOrderMenu.value = false
}

// 切换订单菜单显示状态
const toggleOrderMenu = () => {
  showOrderMenu.value = !showOrderMenu.value
  showAdminMenu.value = false
}

// 点击页面其他位置关闭下拉菜单
const closeDropdownMenus = (event) => {
  const adminDropdown = document.querySelector('.dropdown')
  if (adminDropdown && !adminDropdown.contains(event.target)) {
    showAdminMenu.value = false
    showOrderMenu.value = false
  }
}

// 初始化用户状态
const initializeUser = () => {
  userStore.initializeFromLocalStorage()
  
  // 如果用户未登录且当前不是认证页面，则跳转到登录页
  if (!userStore.isLoggedIn && !isAuthPage.value) {
    router.push('/login')
  }
}

// 处理退出登录
const handleLogout = () => {
  userStore.clearUser()
  router.push('/login')
}

onMounted(() => {
  initializeUser()
  // 添加点击事件监听器，实现点击页面其他位置关闭下拉菜单
  document.addEventListener('click', closeDropdownMenus)
})
</script>

<template>
  <div class="layout-container">
    <!-- 只在非登录/注册页面显示导航栏 -->
    <header v-if="!isAuthPage" class="bg-primary text-white shadow-md">
      <div class="container mx-auto px-4 py-3">
        <div class="flex justify-between items-center">
          <div class="flex items-center">
            <router-link to="/" class="text-xl font-bold">团购管理系统</router-link>
          </div>
          
          <nav class="flex items-center space-x-6">
            <router-link to="/" class="hover:text-gray-200 transition">商品展示</router-link>
            <router-link to="/group-buy" class="hover:text-gray-200 transition">团购活动</router-link>
            <router-link to="/cart" class="hover:text-gray-200 transition">购物车</router-link>
            <div class="relative">
              <div class="dropdown">
                <span class="hover:text-gray-200 transition cursor-pointer" @click="toggleOrderMenu">我的订单</span>
                <div class="dropdown-content" v-show="showOrderMenu">
                  <router-link to="/orders" class="dropdown-item" @click="showOrderMenu = false">普通订单</router-link>
                  <router-link to="/group-orders" class="dropdown-item" @click="showOrderMenu = false">团购订单</router-link>
                </div>
              </div>
            </div>
            
            <!-- 管理员菜单 -->
            <template v-if="isAdmin">
              <div class="relative">
                <div class="dropdown">
                  <span class="hover:text-gray-200 transition cursor-pointer" @click.stop="toggleAdminMenu">管理功能</span>
                  <div class="dropdown-content" v-show="showAdminMenu">
                    <router-link to="/admin/statistics" class="dropdown-item" @click="showAdminMenu = false">数据统计</router-link>
                    <router-link to="/admin/products" class="dropdown-item" @click="showAdminMenu = false">商品管理</router-link>
                    <router-link to="/admin/users" class="dropdown-item" @click="showAdminMenu = false">用户管理</router-link>
                  </div>
                </div>
              </div>
            </template>
            
            <div class="relative">
              <div class="flex items-center">
                <span class="mr-2">{{ userStore.user?.nickname || '游客' }}</span>
                <button 
                  v-if="userStore.isLoggedIn"
                  @click="handleLogout" 
                  class="px-3 py-1 bg-red-600 text-white rounded hover:bg-red-700 text-sm"
                >
                  退出
                </button>
                <router-link 
                  v-else
                  to="/login" 
                  class="px-3 py-1 bg-blue-600 text-white rounded hover:bg-blue-700 text-sm"
                >
                  登录
                </router-link>
              </div>
            </div>
          </nav>
        </div>
      </div>
    </header>
    
    <main class="flex-grow">
      <router-view></router-view>
    </main>
    
    <footer class="bg-gray-100 py-4 text-center text-gray-600">
      <div class="container mx-auto">
        团购管理系统 &copy; {{ new Date().getFullYear() }}
      </div>
    </footer>
  </div>
</template>

<style scoped>
.layout-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

main {
  flex: 1;
}

a.router-link-active {
  font-weight: bold;
}

.bg-primary {
  background-color: #4CAF50;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

.flex {
  display: flex;
}

.justify-between {
  justify-content: space-between;
}

.items-center {
  align-items: center;
}

.space-x-6 > * + * {
  margin-left: 1.5rem;
}

.text-white {
  color: white;
}

.text-xl {
  font-size: 1.25rem;
}

.font-bold {
  font-weight: bold;
}

.hover\:text-gray-200:hover {
  color: #e5e7eb;
}

.transition {
  transition: all 0.3s ease;
}

.shadow-md {
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

.px-4 {
  padding-left: 1rem;
  padding-right: 1rem;
}

.py-3 {
  padding-top: 0.75rem;
  padding-bottom: 0.75rem;
}

.px-3 {
  padding-left: 0.75rem;
  padding-right: 0.75rem;
}

.py-1 {
  padding-top: 0.25rem;
  padding-bottom: 0.25rem;
}

.mr-2 {
  margin-right: 0.5rem;
}

.rounded {
  border-radius: 0.25rem;
}

.bg-red-600 {
  background-color: #dc2626;
}

.hover\:bg-red-700:hover {
  background-color: #b91c1c;
}

.bg-blue-600 {
  background-color: #2563eb;
}

.hover\:bg-blue-700:hover {
  background-color: #1d4ed8;
}

.text-sm {
  font-size: 0.875rem;
}

.bg-gray-100 {
  background-color: #f3f4f6;
}

.py-4 {
  padding-top: 1rem;
  padding-bottom: 1rem;
}

.text-center {
  text-align: center;
}

.text-gray-600 {
  color: #4b5563;
}

.cursor-pointer {
  cursor: pointer;
}

/* 下拉菜单样式 */
.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  position: absolute;
  background-color: white;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 3500;
  border-radius: 4px;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  padding-top: 10px;
  margin-top: 5px;
  border: 1px solid #e4e7ed;
}

.dropdown-item {
  color: #333;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
  transition: background-color 0.3s;
}

.dropdown-item:hover {
  background-color: #f5f7fa;
  color: #4CAF50;
}

.dropdown-content::before {
  content: "";
  position: absolute;
  top: -5px;
  left: 50%;
  transform: translateX(-50%);
  border-width: 0 5px 5px;
  border-style: solid;
  border-color: transparent transparent white;
  z-index: 1;
}
</style>
